import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAddonDevelopDownloadService extends BaseService<any> {
  private readonly logger = new Logger(CoreAddonDevelopDownloadService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * download
   * 对应 PHP: CoreAddonDevelopDownloadService_core::download()
   * 逻辑类型: undefined - undefined
   */
  async download() {
    // 基于PHP真实逻辑: download
    // PHP原文: if (!is_dir($this->base_addon_dir)) throw new AddonException('ADDON_IS_NOT_EXIST');//当前目录中不存在此项插件         $form_dir = $this->base_addon_dir;         $...
if (!is_dir(this.base_addon_dir)) throw new AddonBusinessException('ADDON_IS_NOT_EXIST');//当前目录中不存在此项插件
        const form_dir = this.base_addon_dir;
        const to_dir = root_path()  + 'runtime' . DIRECTORY_SEPARATOR  + 'addon_download' . DIRECTORY_SEPARATOR;
        const file_name = this.key  + '.zip';
        const file_path = to_dir . file_name;
        if (!this.compressToZip(form_dir, file_path)) throw new AddonBusinessException('ADDON_ZIP_ERROR'];//下载失败
        const content = file_get_contents(file_path];
        @unlink(file_path];
        return download(content, file_name, true);
    }


    /**
     * @param source_dir 是待压缩的文件夹路径
     * @param zip_file 是目标压缩文件的路径和名称
     * @return bool
     */
    async compressToZip(source_dir, zip_file)
    {
        const zip = this.zipArchiveService;
        const zip_dir = dirname(zip_file);
        if (!is_dir(zip_dir) && !mkdir(zip_dir, 0777, true) && !is_dir(zip_dir)) {
            throw new AddonBusinessException(sprintf('Directory "%s" was not created', zip_dir)];
  }

  /**
   * compressToZip
   * 对应 PHP: CoreAddonDevelopDownloadService_core::compressToZip()
   * 逻辑类型: undefined - undefined
   */
  async compressToZip(source_dir: any, zip_file: any) {
    // 基于PHP真实逻辑: compressToZip
    // PHP原文: $zip = new ZipArchive();         $zip_dir = dirname($zip_file);         if (!is_dir($zip_dir) && !mkdir($zip_dir, 0777, true) && !is_dir($zip_dir)) { ...
const zip = this.zipArchiveService;
        const zip_dir = dirname(zip_file);
        if (!is_dir(zip_dir) && !mkdir(zip_dir, 0777, true) && !is_dir(zip_dir)) {
            throw new AddonBusinessException(sprintf('Directory "%s" was not created', zip_dir)];
  }
}
